﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JXT.PrimaryKey.Batman.Domain.Quotation.Repositories;
using JXT.PrimaryKey.Batman.Domain.Quotation.Models;
using JXT.PrimaryKey.Batman.Domain.Repositories.MySql;
using JXT.PrimaryKey.Batman.Domain.Specifications;
using System.Data;

namespace JXT.PrimaryKey.Batman.Domain.Repositories.Quotation
{
    public class MySqlMarketStatisticStatusRepository : MySqlRepositoryBase<MarketStatisticStatus, int>, IMarketStatisticStatusRepository
    {
        public MySqlMarketStatisticStatusRepository(RepositoryContext context)
            : base((MySqlRepositoryContext)context)
        { }
        public MarketStatisticStatus GetMarketstatisticstatus(ISpecification<MarketStatisticStatus> specification)
        {
            var expressionSpecifcation = specification.ToExpressionSpecification();
            IDataParameter[] parameters = null;
            string sql = expressionSpecifcation.BuildSingleQueryString(out parameters);
            MarketStatisticStatus result = null; //必须将初始值设置为null
            using (var dr = Context.ExecuteDataReader(sql, parameters))
            {
                if (dr.Read())
                {
                    result = ReadFromDataReader<MarketStatisticStatus>(dr);
                }
            }
            return result;
        }





        public MarketStatisticStatus GetMarketstatisticstatus(MarketType markettype, TimeSortType timesort, DateTime dt)
        {
            int markettypeValue = (int)markettype;
            int timesortvalue = (int)timesort;
            var sql = "select * from `marketstatisticstatus` where `markettype`=@markettype and `timesorttype`=@timesort and `updatetime`=@dt";
            MarketStatisticStatus result = null;
            var parameters = new[]
            {
                Context.CreateParameter("@markettype",markettypeValue),
                Context.CreateParameter("@timesort",timesortvalue),
                Context.CreateParameter("@dt",dt.Date)
            };
            using (var dr = Context.ExecuteDataReader(sql, parameters)) 
            {
                if (dr.Read())
                {
                    //result = new MarketStatisticStatus();
                    result = ReadFromDataReader<MarketStatisticStatus>(dr);
                    //result.Id = dr.Read<int>("id");
                    //result.Markettype = dr.Read<MarketType>("markettype");
                    //result.Status = dr.Read<bool>("status");
                    //result.TimeSorttype = dr.Read<TimeSortType>("timesorttype");
                    //result.UpdateTime = dr.Read<DateTime>("updatetime");
                }
            }
            return result;
        }
    }
}
